<template>
  <el-card style="max-width: 480px;margin: 200px auto">
    <h1>欢迎使用电商管理系统</h1>
    <el-form :model="form" label-width="auto" style="max-width: 480px">
      <el-form-item label="账号">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登 录</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>
IndexView
<script>
import { ElMessage } from 'element-plus'
export default {
  name: "LoginView",
  data(){
    return {
      form : {username:'',password:''}
    }
  },
  methods:{
    login(){
      //使用axios发送登录post请求
      this.$axios.post('/login',this.$qs.stringify(this.form))
          .then(result => {
            console.log('login',result.data)
            //判断登录是否成功
            if(result.data.user) {
              ElMessage.success('登录成功')
              //保存用户的信息
              localStorage.setItem('user', JSON.stringify(result.data.user))
              //保存token
              localStorage.setItem('token', result.data.token)
              this.$router.push('/index')
            }
            })
    }
  }
}
</script>
<style scoped>
</style>